package com.bs.process.project.controller;

import com.alibaba.fastjson.JSON;
import com.bs.common.config.Global;
import com.bs.common.utils.DateUtils;
import com.bs.common.utils.StringUtils;
import com.bs.process.project.domain.SysProject;
import com.bs.process.project.domain.SysProjectFile;
import com.bs.process.project.domain.SysProjectFileType;
import com.bs.process.project.domain.Tree;
import com.bs.process.project.service.ISysProjectFileService;
import com.bs.process.project.service.ISysProjectFileTypeService;
import com.bs.process.project.service.ISysProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.misc.BASE64Encoder;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;


/**
 * @author jiangfeng
 * @version 1.0
 * @date 2020-06-13 18:56
 */
@Controller
@RequestMapping("/process/project/scanCode")
public class ScanCodeController {

    private String prefix = "process/project/project";

    @Autowired
    private ISysProjectFileTypeService sysProjectFileTypeService;

    @Autowired
    private ISysProjectService sysProjectService;

    @Autowired
    private ISysProjectFileService sysProjectFileService;

    /**
     * 封面
     *
     * @param pId
     * @param mmap
     * @return
     */
    @RequestMapping
    public String scanCode(@RequestParam(value = "pId", defaultValue = "1") long pId,
                           ModelMap mmap) {
        mmap.put("pId", pId);
        return prefix + "/projectCover";
    }

    /**
     * 获取项目信息
     *
     * @param pId
     * @param mmap
     * @return
     */
    @RequestMapping("/projectInfo/{pId}")
    public String findProjectInfo(@PathVariable(value = "pId") long pId,
                                  ModelMap mmap) {
        SysProject sysProject = sysProjectService.selectSysProjectById(pId);
        if (sysProject == null) {
            return prefix + "/noProject";
        }
        mmap.put("sysProject", sysProject);
        return prefix + "/projectInfo";
    }

    /**
     * 获取项目附件
     *
     * @param pId
     * @param mmap
     * @return
     */
    @RequestMapping("/projectFile/{pId}")
    public String findProjectFile(@PathVariable(value = "pId") long pId,
                                  ModelMap mmap) {
        List<SysProjectFile> sysProjectFiles = sysProjectFileService.selectSysProjectFileList(new SysProjectFile(pId));

        List<Tree<SysProjectFile>> list = new ArrayList<Tree<SysProjectFile>>();
        List<Tree<SysProjectFile>> trees = new ArrayList<Tree<SysProjectFile>>();

        for (SysProjectFile sysProjectFile : sysProjectFiles) {
            try {
                String url = sysProjectFile.getUrl();
                Tree<SysProjectFile> tree = new Tree<SysProjectFile>();

//                if (!"#".equals(url) && StringUtils.isNotNull(url)) {
//                    String profile = Global.getProfile();
//                    String fileUrl = "/upload/uploadPath" + url.split(profile)[1];
//                    tree.setImgData(fileUrl);
//                }
                String fileUrl = "/upload/uploadPath" + url;
                System.out.println("文件地址："+fileUrl);
                tree.setImgData(fileUrl);
                
                tree.setId(sysProjectFile.getId().toString());
                tree.setParentId(sysProjectFile.getParentId().toString());
                tree.setName(sysProjectFile.getFileName());
                tree.setFileDir(sysProjectFile.getUrl());
                tree.setMenuType(sysProjectFile.getStatus());
                Date expirationTime = sysProjectFile.getExpirationTime();
                if (expirationTime != null) {
                    tree.setExpirationTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, expirationTime));
                    if (new Date().getTime() - expirationTime.getTime() > 0) {
                        tree.setOverdue("1");
                    }
                }
                trees.add(tree);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        list = buildList(trees, "0");
        System.out.println(JSON.toJSON(list));
        mmap.put("sysProjectFiles", list);
        return prefix + "/projectFile";
    }

    private <T> List<Tree<T>> buildList(List<Tree<T>> nodes, String idParam) {
        if (nodes == null) {
            return null;
        }

        List<Tree<T>> topNodes = new ArrayList<Tree<T>>();

        for (Tree<T> children : nodes) {
            String pid = children.getParentId();
            if (pid == null || idParam.equals(pid)) {
                topNodes.add(children);
                continue;
            }

            for (Tree<T> parent : nodes) {
                String id = parent.getId();
                if (id != null && id.equals(pid)) {
                    children.setParentName(parent.getName());
                    parent.getChildren().add(children);
                    continue;
                }
            }
        }
        return topNodes;
    }
}
